﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LumenLiquid.Font;

/***********************************
 *      CShark & VisibleBasix      *
 ***********************************/

namespace LumenLiquid._2D
{
    /// <summary>
    /// Supports multiple lines with multiple fonts but only one per line
    /// Not for instantuse!
    /// </summary>
    public class Textblock
    {
        /// <summary>
        /// Contains detailed infos for each single line
        /// </summary>
        private struct sLine
        {
            public string Text;
            public string Font;
            public Color color;
            public Vector2 Position;
        }
        /// <summary>
        /// Contains information for one or more lines of text
        /// </summary>
        public struct sLineInfo
        {
            public string Text;
            public string Font;
            public Color color;
        }

        /// <summary>
        /// The Drawable area of the text
        /// </summary>
        Rectangle TextArea;
        /// <summary>
        /// The top position of the first line
        /// </summary>
        int Y;
        /// <summary>
        /// Contains every single Line with all Informations
        /// </summary>
        private List<sLine> _textblock;


        /// <summary>
        /// Initializes the Textblock
        /// </summary>
        /// <param name="Text">The Text to handle (Line Splitter is "\n\r")</param>
        /// <param name="Font">The font for the whole block</param>
        /// <param name="Color">The color for the whole text</param>
        /// <param name="TextAligin">The Aligin of the Text</param>
        /// <param name="TextArea">The Area where the Text can been shown</param>
        /// <param name="LineSpace">Space between the lines</param>
        public Textblock(string Text, string Font, Color Color, Global.Menu._2DAligin TextAligin, Rectangle TextArea, int LineSpace)
        {
            List<string> TextLines = Text.Split(new string[] { "\n\r" }, StringSplitOptions.None).ToList();
            _textblock = new List<sLine>();
            sLine temp;
            int Y = 0, X;

            //Calculate Position for every single line
            for (int i = 0; i < TextLines.Count; i++)
            {
                temp = new sLine();
                temp.Text = TextLines[i];
                temp.Font = Font;
                temp.color = Color;

                //Calculate the X Value out of the aligin
                X = TextArea.X;
                switch (TextAligin)
                {
                    case Global.Menu._2DAligin.Left:
                        X = TextArea.X;
                        break;
                    case Global.Menu._2DAligin.Middle:
                        X = (TextArea.X + TextArea.Width / 2) - ((int)Global.FontManager[temp.Font].MeasureString(temp.Text).X / 2);
                        break;
                    case Global.Menu._2DAligin.Right:
                        X = TextArea.X + TextArea.Width - (int)Global.FontManager[temp.Font].MeasureString(temp.Text).X;
                        break;
                }

                temp.Position = new Vector2(X, Y);

                Y += (int)Global.FontManager[temp.Font].MeasureString(temp.Text).Y + LineSpace;

                _textblock.Add(temp);
            }

            this.TextArea = TextArea;
        }

        /// <summary>
        /// Initializes the Textblock
        /// </summary>
        /// <param name="Text">The Lines of Text to handle (Line Splitter is "\n\r")</param>
        /// <param name="TextAligin">The Aligin of the Text</param>
        /// <param name="TextArea">The Area where the Text can been shown</param>
        /// <param name="LineSpace">Space between the lines</param>
        public Textblock(List<sLineInfo> Text, Global.Menu._2DAligin TextAligin, Rectangle TextArea, int LineSpace)
        {
            int Y = 0, X;
            sLine temp;
            List<string> InnerLines;

            for (int i = 0; i < Text.Count; i++)
            {
                InnerLines = new List<string>();

                InnerLines = Text[i].Text.Split(new string[] { "\n\r" }, StringSplitOptions.None).ToList();

                for (int i2 = 0; i2 < InnerLines.Count; i2++)
                {
                    temp = new sLine();
                    temp.Text = InnerLines[i2];
                    temp.Font = Text[i].Font;
                    temp.color = Text[i].color;

                    //Calculate the X Value out of the aligin
                    X = TextArea.X;
                    switch (TextAligin)
                    {
                        case Global.Menu._2DAligin.Left:
                            X = TextArea.X;
                            break;
                        case Global.Menu._2DAligin.Middle:
                            X = (TextArea.X + TextArea.Width / 2) - ((int)Global.FontManager[temp.Font].MeasureString(temp.Text).X / 2);
                            break;
                        case Global.Menu._2DAligin.Right:
                            X = TextArea.X + TextArea.Width - (int)Global.FontManager[temp.Font].MeasureString(temp.Text).X;
                            break;
                    }

                    temp.Position = new Vector2(X, Y);

                    Y += (int)Global.FontManager[temp.Font].MeasureString(temp.Text).Y + LineSpace;

                    _textblock.Add(temp);
                }
            }

            this.TextArea = TextArea;
        }

        public void Update(GameTime gametime, int YValue)
        {
            Y = YValue;
        }

        public void Draw(GameTime gametime)
        {
            foreach (sLine line in _textblock)
            {
                if ((line.Position + new Vector2(0, Y)).Y > TextArea.Bottom)
                    break;

                if ((line.Position + new Vector2(0, Y) + Global.FontManager[line.Font].MeasureString(line.Text)).Y >= TextArea.Top)
                    Global.FontManager[line.Font].DrawString(line.Text, line.Position + new Vector2(0, Y), line.color);
            }
        }

    }
}
